package com.prs.crm.action.stock.purchase;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.project.ProjectType;
import com.prs.crm.domain.stock.SupplierType;
import com.prs.crm.service.stock.SupplierTypeService;
import com.prs.crm.service.sys.LogService;

public class SupplierTypeTypeAction extends BaseAction{
	private static final long serialVersionUID = 1L;

	private SupplierTypeService supplierTypeService;
	
	private SupplierType type;
	
	private String typeName;
	private LogService logService;
	
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	private PaginationSupport pagination;
	
	private Integer[] typeIds;

	public void setSupplierTypeService(SupplierTypeService supplierTypeService) {
		this.supplierTypeService = supplierTypeService;
	}

	public SupplierTypeService getSupplierTypeService() {
		return supplierTypeService;
	}

	public void setType(SupplierType type) {
		this.type = type;
	}

	public SupplierType getType() {
		return type;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}
	
	@Action("listSupplierType")
	public String listSupplierType()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SupplierType.class);
		this.setPagination(this.getSupplierTypeService().listByPage(criteria,
							getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	
	@Action("editSupplierType")
	public String editSupplierType()
	{
		return SUCCESS;
	}
	
	@Action(value="saveSupplierType",results={@Result(name="success",type="redirect",location="listSupplierType"),
			@Result(name="error",type="chain",location="editSupplierType")})
	public String saveSupplierType()
	{
		if(type.getName()==null||type.getName().equals("")){
			this.addActionError("名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(SupplierType.class,
				type.getName())) {
			if(!(type.getName().equals(typeName))){
				this.addActionError("名称已经存在");
				return ERROR;
			}
		}
		    this.getSupplierTypeService().save(type);
		return SUCCESS;
	}
	
	@Action(value="removeSupplierType",results={@Result(name="success",type="redirect",location="listSupplierType")})
	public String removeSupplierType()
	{
		this.getSupplierTypeService().batchRemove(typeIds);
		return SUCCESS;
	}
}
